package com.example.figurehecheng.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.figurehecheng.domain.entity.Subscribe;
import com.example.figurehecheng.mapper.SubscribeMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class SubscribeTask {

    private final SubscribeMapper subscribeMapper;


    /**
     * 定时处理预约待完成预约单
     */
//    @Scheduled(cron = "0 0 1 * * ?") //每天凌晨1点触发一次
    @Scheduled(cron = "0 * * * * ?")
    public void processDeliverySubscribe(){
        log.info("定时处理预约待完成预约单：{}", LocalDateTime.now());
        //查询为完成预约单
        List<Subscribe> subscribes = subscribeMapper.selectList(new LambdaQueryWrapper<Subscribe>()
                .in(Subscribe::getState, Subscribe.AGREE, Subscribe.SUBJECT_TO_CONSENT)
                .lt(Subscribe::getEndTime, LocalDateTime.now()));
        if (subscribes != null && subscribes.size() > 0){
            for (Subscribe subscribe : subscribes) {
                if (subscribe.getState() == 1){
                    subscribe.setState(Subscribe.DONE);
                    //修改预约单为已完成
                    subscribeMapper.updateById(subscribe);
                }else {
                    subscribe.setState(Subscribe.REFUSE);
                    //修改预约单为失败
                    subscribeMapper.updateById(subscribe);
                }

            }
        }

    }

}
